package com.chuanyang.driverh.mainhome.fragment;

import android.view.View;

import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.base.BaseListFragment;
import com.chuanyang.driverh.mainhome.MoneyFollowWaybilListActivity;
import com.chuanyang.driverh.mainhome.adapter.MoneyFollowWaybilListAdapter;
import com.chuanyang.driverh.responseresult.LookWaybillResult;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.eventbus.RefleshFragEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class MoneyFollowWaybilListFragment extends BaseListFragment {
    private MoneyFollowWaybilListAdapter listAdapter;
    private String orderId;

    @Override
    public BaseListAdapter createAdapter() {
        return listAdapter = new MoneyFollowWaybilListAdapter(getActivity());
    }

    @Override
    public void initView(View view) {
        super.initView(view);

    }

    @Override
    protected void initData() {
        orderId = ((MoneyFollowWaybilListActivity)getActivity()).getOrderId();
                getData();
     /*   ResultMoneyFollow.OutData.MiddleData.InnderData data= (ResultMoneyFollow.OutData.MiddleData.InnderData) ((MoneyFollowWaybilListActivity)getActivity()).getIntent().getSerializableExtra("data");


        try {
            setDataResult(data.getWaybills());
        } catch (Exception e) {
            listAdapter.clear();
            listAdapter.notifyDataSetChanged();
        }*/
    }

    @Override
    protected void initListener() {

    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

   /* private void getData() {
        DialogUtil.showLoadingDialog(getActivity(), "加载中...");
        Map<String, String> params = new HashMap<String, String>();
        params.put("orderId", orderId);
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("page", mCurrentPage + "");
        params.put("length", "10");
        MyHttpUtil.waybilList(getActivity(), params, new MyCallback<ResultGoodsAllot>(getActivity(), new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                springView.onFinishFreshAndLoad();
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(ResultGoodsAllot result, int id) {
                if (result.getCode() == ConstUtil.OK && result.getData()!=null) {
                    try {
                        setDataResult(result.getData());
                    } catch (Exception e) {
                        if (result.getMsg()!=null){
                            PrintUtil.toast(activityContext,result.getMsg());
                        }
                        listAdapter.clear();
                        listAdapter.notifyDataSetChanged();
                    }
                } else {
                    springView.onFinishFreshAndLoad();
                    PrintUtil.toast(getActivity(), result.getMsg());
                }
            }
        });
    }*/


    //根据订单ID查看运单
    private void getData() {
        DialogUtil.showLoadingDialog(activityContext,"加载中...");
        Map<String,Object> params=new HashMap<String, Object>();
        params.put("orderId", orderId);
        params.put("page", mCurrentPage);
        params.put("length", ConstUtil.PAGE_SIZE);
        MyHttpUtil.selectByOrderId(activityContext,params, new MyCallback<LookWaybillResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(LookWaybillResult result, int id) {
                if (result.getCode() == ConstUtil.OK)
                {
                    try {
//                        List<LookWaybillData.DataBean> ss = result.getData().getData();
                        setDataResult(result.getData().getData());
                    } catch (Exception e) {
                        if (result.getMsg()!=null){
                            PrintUtil.toast(activityContext,result.getMsg());
                        }
                        listAdapter.clear();
                        listAdapter.notifyDataSetChanged();
                    }
                } else {
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });
    }

    @Override
    public void onGetMore() {
        getData();
    }

    @Override
    public void onRefresh() {
        mCurrentPage = 1;
        getData();
    }

    @Override
    protected void onForceRefresh() {
        onRefresh();
    }

    @Override
    protected boolean isLoadMore() {
        return true;
    }

    @Override
    protected boolean isPullToRefresh() {
        return true;
    }

    public void onEventMainThread(RefleshFragEvent event) {
        if (event.getIndex()==5 ){
            onRefresh();
        }
    }
}
